package com.cnaaa.xinqiao.controller;

import com.cnaaa.xinqiao.dto.PageParam;
import com.cnaaa.xinqiao.service.MemberService;
import com.cnaaa.xinqiao.util.CollectionUtil;
import com.cnaaa.xinqiao.vo.BasicJsonResult;
import com.cnaaa.xinqiao.vo.MemberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 2018/10/19 Create by 郭文梁
 * MemberController
 * 用户访问控制器
 *
 * @author 郭文梁
 * @date 2018/10/19
 */
@RestController
@RequestMapping("/member")
public class MemberController {
    private final MemberService memberService;

    @Autowired
    public MemberController(MemberService memberService) {
        this.memberService = memberService;
    }

    /**
     * 分页查询用户
     *
     * @param page 页数
     * @param size 页大小
     * @return Member List
     */
    @GetMapping("/list/{page}")
    public BasicJsonResult<List<MemberVo>> listPage(@PathVariable("page") Integer page, @RequestParam("size") int size) {
        if (page > 0 && size >= 0) {
            List<MemberVo> members = CollectionUtil.mapAsList(memberService.listPage(page, size), MemberVo::of);
            return BasicJsonResult.ok(members);
        } else {
            return BasicJsonResult.error(1, "参数错误");
        }
    }

    /**
     * 各区人口数
     *
     * @return {
     * 区：人数
     * }
     */
    @GetMapping("/numberOfPeoplePerDistrict")
    public Map<String, Integer> numberOfPeoplePerDistrict() {
        return memberService.numberOfPeoplePerDistrict();
    }

    /**
     * 分页查询 支持POST查询
     *
     * @param pageParam 页参数
     * @return BJR
     */
    @PostMapping("/list")
    public BasicJsonResult<List<MemberVo>> listPage(@RequestBody PageParam pageParam) {
        return listPage(pageParam.getPage(), pageParam.getSize());
    }
}
